# -*- encoding=utf8 -*-
__author__ = "Bryan.Chen"

from airtest.core.api import *

auto_setup(__file__)

from poco.drivers.std import StdPoco
poco = StdPoco()

using("../FlowTest.air")
import FlowTest

class RedMatch_EnterMatch(FlowTest.BaseTestCase):

    def __init__(self):
        print("%s __init__" % (__file__))
    
    def runTest(self, args):

        matchName = "勿动-6人测试"
        
        if "matchName" in args:
            matchName = args["matchName"]
        
        try:
            poco("SelectMatchLayer").wait_for_appearance(120)
        except:
            print(u"打开比赛列表超时")
            
        assert_equal(poco("SelectMatchLayer").exists(), True, "打开比赛列表")
        
        sleep(1.0)

        matchListView = poco("SelectMatchLayer").offspring("match_listview")
        matchItems = matchListView.children()
        assert_equal(len(matchItems) > 0, True, "比赛列表是否拉取成功")
        
        # poco.swipe([0.5, 0.5], [0.5, -0.2])  
        # sleep(5.0)

        isFreeMatch = False        
        isFindMatch = False
        for item in matchItems:
            txtMatchName = item.offspring("Text_matchName").get_text()
            txtMatchName = txtMatchName.replace(" ", "")
            if txtMatchName == matchName:
                isFreeMatch = item.offspring("Text_nofee").exists()
                self.clickButton(item)
                break
        
        try:
            poco("DetailMatchLayer").wait_for_appearance()
        except:
            print("打开赛事详情失败")
        assert_equal(poco("DetailMatchLayer").exists(), True, "打开赛事详情")
            
        detailLayer = poco("DetailMatchLayer")
        btnSignIn = detailLayer.offspring("Button_apply")
        self.clickButton(btnSignIn)

        if not isFreeMatch:
            try:
                poco("RMTipsLayer").wait_for_appearance()
            except:
                assert_equal(poco("RMTipsLayer").exists(), True, "打开比赛报名确认框")
                return False
            
            btnSure = poco("RMTipsLayer").offspring("Button_left")
            self.clickButton(btnSure)
        
        return True
        
_instance = RedMatch_EnterMatch()
runTest = _instance.runTest

